package net.zhengxinyun.performance.controller.measurement;

import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.*;
import net.zhengxinyun.performance.filter.AnswerFilter;
import net.zhengxinyun.performance.filter.MeasurementInformationFilter;
import net.zhengxinyun.performance.filter.QuestionFilter;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.measurement.AnswerService;
import net.zhengxinyun.performance.service.measurement.AssessmentTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hyp1202 on 2019/1/11.
 */
@RestController
@RequestMapping("/answer")
public class AnswerController {

    private AssessmentTestService assessmentTestService;
    private AnswerService answerService;
    private TokenService tokenService;
    private LoginService loginService;

    @Autowired
    public AnswerController(AssessmentTestService assessmentTestService, AnswerService answerService, TokenService tokenService, LoginService loginService) {
        this.assessmentTestService = assessmentTestService;
        this.answerService = answerService;
        this.tokenService = tokenService;
        this.loginService = loginService;
    }

    @PostMapping("/query")
    public Result<HashMap<String, Object>> query(@RequestBody AnswerParam param) {
        HashMap<String, Object> map = new HashMap<>();
        List<MeasurementInformationFilter> list = assessmentTestService.query(param.measurementInformationFilter);
        if (list.size() == 0) {
            param.measurementInformationFilter.setPageNumber(param.measurementInformationFilter.getPageNumber() - 1);
            list = assessmentTestService.query(param.measurementInformationFilter);
        }
        int pageTotal = assessmentTestService.queryForCount(param.measurementInformationFilter);
        map.put("list", list);
        map.put("pageTotal", pageTotal);
        return ResultUtils.getSuccessResults(map);
    }

    @PostMapping("/queryForPaper")
    public Result<HashMap<String, Object>> queryForPaper(@RequestBody AnswerParam param) {
        HashMap<String, Object> map;
        if (param.evaluateUserEntity == null || "".equals(param.evaluateUserEntity.getPhone())) {
            map = assessmentTestService.queryForPaper(param.measurementInformationFilter);
        } else {
            try {
                map = assessmentTestService.queryForPaper(param.measurementInformationFilter, param.evaluateUserEntity);
            } catch (Exception e) {
                return ResultUtils.getResults(Result.Status.ERROR, "已进行过此份测评", null);
            }
        }
        return ResultUtils.getSuccessResults(map);
    }

    @PostMapping("/answer")
    public Result<String> answer(@RequestBody AnswerParam param) {
        String str;
        if (param.evaluateUserEntity == null) {
            str = answerService.insert(param.measurementInformationEntity, param.answerList);
        } else {
            str = answerService.insert(param.measurementInformationEntity, param.answerList, param.evaluateUserEntity);
        }
        if (!"答题成功".equals(str)) {
            return ResultUtils.getResults(Result.Status.ERROR, str);
        }
        return ResultUtils.getSuccessResults();
    }

    @PostMapping("/login")
    public Result<ArrayList<MeasurementInformationFilter>> login(@RequestBody AnswerParam param) {
        List<MeasurementInformationFilter> list = answerService.login(param.evaluateUserEntity.getPhone());
        if (list == null || list.size() == 0) {
            return ResultUtils.getResults(Result.Status.ERROR, "没有该账号或没有对应测评，请联系管理员", null);
        }
        return ResultUtils.getSuccessResultList(list);
    }

    @PostMapping("/noLogin")
    public Result<ArrayList<MeasurementInformationFilter>> noLogin() {
        List<MeasurementInformationFilter> list = answerService.noLogin();
        if (list == null || list.size() == 0) {
            return ResultUtils.getResults(Result.Status.ERROR, "没有需要的测评，请联系管理员", null);
        }
        return ResultUtils.getSuccessResultList(list);
    }

    @PostMapping("/queryForAnswerCount")
    public Result<ArrayList<AnswerFilter>> queryForAnswerCount(@RequestBody AnswerParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            ArrayList<AnswerFilter> list = (ArrayList<AnswerFilter>) answerService.queryForAnswerCount(param.answerFilter);
            return ResultUtils.getSuccessResults(list);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }


}

class AnswerParam {
    public AnswerEntity answerEntity;
    public AnswerFilter answerFilter;
    public MeasurementInformationFilter measurementInformationFilter;
    public MeasurementInformationEntity measurementInformationEntity;
    public QuestionEntity questionEntity;
    public QuestionFilter questionFilter;
    public List<AnswerEntity> answerList;
    public EvaluateUserEntity evaluateUserEntity;
    public TokenEntity tokenEntity;
}
